//
//  UIImageView+Download.m
//  BuDeJie
//
//  Created by sunny on 16/4/16.
//  Copyright © 2016年 sunny. All rights reserved.
//

#import "UIImageView+Download.h"
#import <AFNetworkReachabilityManager.h>

@implementation UIImageView (Download)
- (void)gq_setImageWithOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageUrl:(NSString *)thumbnailImageUrl placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completedBlock
{
    UIImage *originImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:OriginalImageURL];//从内存/沙盒中取原始图片
    if (originImage) {  //内存/沙盒中有原始图片
        [self sd_setImageWithURL:[NSURL URLWithString:OriginalImageURL] completed:completedBlock];
    } else {
        //            根据网络状态下载图片
        AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
        if (manager.isReachableViaWWAN) {//用户的网络处于3G,4G状态
            
            //            假设用户设置3g,4g状态下仍然下载大图的key值为alwaysDownloadOriginImage
            //     用户的配置项假设利用NSUserDefaults存储到了沙盒中
            //    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"alwaysDownloadOriginalImage"];
            //    [[NSUserDefaults standardUserDefaults] synchronize];
// 从沙盒中读取用户的配置项：在3G\4G环境是否仍然下载原图
            BOOL alwaysDownloadOriginImage = [[NSUserDefaults standardUserDefaults] boolForKey:@"alwaysDownloadOriginImage"];
            if (alwaysDownloadOriginImage) { //如果用户设置了3g,4g状态下仍然下载大图
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageUrl]placeholderImage:placeholderImage completed:completedBlock];
            } else {
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageUrl] placeholderImage:placeholderImage completed:completedBlock];
            }
            
        } else if (manager.isReachableViaWiFi) { //用户的网络处于wifi状态
            [self sd_setImageWithURL:[NSURL URLWithString:OriginalImageURL] placeholderImage:placeholderImage];
        } else {
            UIImage *thumbnailImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:thumbnailImageUrl]; //从内存中取小图片
            if (thumbnailImage) { //内存中有小图
                [self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageUrl] completed:completedBlock];
            } else {
                //                    防止cell循环利用，清空图片
                [self sd_setImageWithURL:nil placeholderImage:placeholderImage completed:completedBlock];
            }
        }
    }

}

- (void)gq_setImageWithOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageUrl:(NSString *)thumbnailImageUrl placeholderImage:(UIImage *)placeholderImage
{
    [self gq_setImageWithOriginalImageURL:OriginalImageURL thumbnailImageUrl:thumbnailImageUrl placeholderImage:placeholderImage completed:nil];
}

- (void)gq_setImageWithOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageUrl:(NSString *)thumbnailImageUrl
{
    [self gq_setImageWithOriginalImageURL:OriginalImageURL thumbnailImageUrl:thumbnailImageUrl placeholderImage:nil completed:nil];
}

- (void)gq_setImageWithOriginalImageURL:(NSString *)OriginalImageURL thumbnailImageUrl:(NSString *)thumbnailImageUrl completed:(SDWebImageCompletionBlock)completedBlock
{
    [self gq_setImageWithOriginalImageURL:OriginalImageURL thumbnailImageUrl:thumbnailImageUrl placeholderImage:nil completed:completedBlock];
}
@end
